<template>
  <el-dialog v-bind="$attrs" :close-on-click-modal="false" :modal-append-to-body="false"
    v-on="$listeners" @open="onOpen" fullscreen lock-scroll class="JNPF-full-dialog"
    :show-close="false" :modal="false">
    <div class="JNPF-full-dialog-header">
      <div class="header-title">
        <img src="@/assets/images/jnpf.png" class="header-logo" />
        <p class="header-txt"> · 打印预览</p>
      </div>
      <div class="options">
        <el-button type="primary" size="small" @click="print">打印</el-button>
        <el-button @click="closeDialog()">{{$t('common.cancelButton')}}</el-button>
      </div>
    </div>
    <div class="main" v-loading="loading">
      <div ref="tsPrint" class="print-content" v-html="data" />
    </div>
  </el-dialog>
</template>

<script>
  import { getPrintDevInfo } from '@/api/system/printDev'
  import request from '@/utils/request'
  export default {
    props: ['id','htId'],
    data() {
      return {
        data: '',
        loading: false
      }
    },
    methods: {
      dataInfo(dataAll) {
        let _dataAll = dataAll
        _dataAll.ihis = parseInt(_dataAll.ihis)
        this.dataForm = _dataAll
        this.dataForm.zcgl_htgl_pab_space1 = this.dataForm.zcgl_htgl_pab_space

        for (let i = 0; i < this.dataForm.zcgl_htgl_pab_space1.length; i++) {
          this.dataForm.zcgl_htgl_pab_space1[i].zcname = this.dataForm.zcgl_htgl_pab_space1[i].zcName
          this.dataForm.zcgl_htgl_pab_space1[i].zcnumber = this.dataForm.zcgl_htgl_pab_space1[i].zcNumber
          this.dataForm.zcgl_htgl_pab_space1[i].id = this.dataForm.zcgl_htgl_pab_space1[i].zcId
        }
        for (let i = 0; i < this.dataForm.zcgl_htgl_pab_fytk.length; i++) {
          this.dataForm.zcgl_htgl_pab_fytk[i].relatedSpace = this.dataForm.zcgl_htgl_pab_fytk[i].relatedSpace.split(",")
        }


      },
      onOpen() {
        this.loading = true
        getPrintDevInfo(this.id).then(res => {
          this.data = res.data.printTemplate

          if(this.htId!=null){

            request({
              url: '/api/example/ZcglHtglInfo/' + this.htId,
              method: 'get'
            }).then(res => {
              this.dataInfo(res.data)
              // 处理空值和时间转换的替换逻辑
              function getValue(val) {
                if (val === undefined || val === null) {
                  return "";
                }
                return val;
              }

              function formatDate(timestamp) {
                if (!timestamp) {
                  return "";
                }
                var date = new Date(timestamp);
                var year = date.getFullYear();
                var month = date.getMonth() + 1;
                var day = date.getDate();

                if (month < 10) {
                  month = "0" + month;
                }
                if (day < 10) {
                  day = "0" + day;
                }

                return year + "-" + month + "-" + day;
              }

              var partyInfo = {};
              if (res.data.zcgl_htgl_pab && res.data.zcgl_htgl_pab.length > 0) {
                partyInfo = res.data.zcgl_htgl_pab[0];
              }

              var rentalInfo = {};
              if (res.data.zcgl_htgl_pab_fytk && res.data.zcgl_htgl_pab_fytk.length > 0) {
                rentalInfo = res.data.zcgl_htgl_pab_fytk[0];
              }

              this.data = this.data.replace("{CONTRACT_NUMBER}", getValue(res.data.contractNumber));
              this.data = this.data.replace("{甲方联系人}", getValue(res.data.partyName));
              this.data = this.data.replace("{乙方客户名称}", getValue(partyInfo.pabCustomerName));
              this.data = this.data.replace("{乙方联系地址}", getValue(partyInfo.pabContactAddress));
              this.data = this.data.replace("{乙方证件号码}", getValue(partyInfo.pabIdNo));
              this.data = this.data.replace("{乙方联系电话}", getValue(partyInfo.pabContactNumber));
              this.data = this.data.replace("{合同签订日期}", formatDate(res.data.signingDate));
              this.data = this.data.replace("{租赁面积}", getValue(rentalInfo.rentalArea));
              this.data = this.data.replaceAll("{租赁面积}", getValue(rentalInfo.rentalArea));
              this.data = this.data.replaceAll("{月租金(元)}", getValue(rentalInfo.monthlyRent));
              this.data = this.data.replaceAll("{保证金}", getValue(rentalInfo.earnestMoney));







              this.loading = false
            })


          }


        })
      },
      closeDialog() {
        this.$emit('update:visible', false)
      },
      word() {
        let print = this.$refs.tsPrint.innerHTML
        const blob = new Blob([print], {
          type: ''
        })
        const name = '下载文档.doc'
        this.downloadFile(blob, name)
      },
      downloadFile(data, name, type) {
        let blob = new Blob([data], {
          type: type || ''
        })
        let downloadElement = document.createElement('a')
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = name
        document.body.appendChild(downloadElement)
        downloadElement.click()
        document.body.removeChild(downloadElement)
        window.URL.revokeObjectURL(href)
      },
      print() {
        let print = this.$refs.tsPrint.innerHTML
        let newWindow = window.open('_blank')
        newWindow.document.body.innerHTML = print
        newWindow.print()
        newWindow.close()
      },
    }
  }
</script>
<style lang="scss" scoped>
  .print-content {
    background: white;
    padding: 40px 30px;
    margin: 0 auto;
    border-radius: 4px;
    width: 776px;
    height: 100%;
    overflow: hidden auto;
  }
</style>
